#include "openmove_Prefix.h"

#include "SceneBase.h"
#include "SceneEcriture.h"
#include "SceneCubenu.h"
#include "SceneLoupVoyeur.h"
#include "ScenePoursuite.h"
#include "SceneWater.h"
#include "ScenePluie.h"
#include "SceneRencontre.h"
#include "SceneInBed.h"
#include "SceneInBedHaut.h"
#include "ScenePetitPot.h"
#include "SceneGlamPetitLoup.h"
#include "SceneSouffle.h"
#include "SceneBlanche.h"
#include "SceneFugana.h"
#include "SceneTestParticule.h"
#include "SceneTestRenaissance.h"
#include "SceneTestFur.h"


XERCES_CPP_NAMESPACE_USE

namespace Utils
{
void initializeFromXml(char const * const xmlFile, std::vector<CSceneBase *> &sceneMap, COgreEngine *engine, int const &netConfig)
{
	XMLCh tempStr[100];
	XMLString::transcode("LS", tempStr, 99);
	DOMImplementation *impl = DOMImplementationRegistry::getDOMImplementation(tempStr);
	DOMBuilder* parser = ((DOMImplementationLS*)impl)->createDOMBuilder(DOMImplementationLS::MODE_SYNCHRONOUS, 0);

	// optionally you can set some features on this builder
	if (parser->canSetFeature(XMLUni::fgDOMValidation, true))
		parser->setFeature(XMLUni::fgDOMValidation, true);
	if (parser->canSetFeature(XMLUni::fgDOMNamespaces, true))
		parser->setFeature(XMLUni::fgDOMNamespaces, true);
	if (parser->canSetFeature(XMLUni::fgDOMDatatypeNormalization, true))
		parser->setFeature(XMLUni::fgDOMDatatypeNormalization, true);
	if (parser->canSetFeature(XMLUni::fgDOMDatatypeNormalization, true))
		parser->setFeature(XMLUni::fgDOMDatatypeNormalization, true);
	if (parser->canSetFeature(XMLUni::fgXercesDOMHasPSVIInfo, true))
		parser->setFeature(XMLUni::fgXercesDOMHasPSVIInfo, true);
	if (parser->canSetFeature(XMLUni::fgDOMWhitespaceInElementContent, true))
		parser->setFeature(XMLUni::fgDOMWhitespaceInElementContent, false);
	if (parser->canSetFeature(XMLUni::fgXercesSchema, true))
		parser->setFeature(XMLUni::fgXercesSchema, true);
	if (parser->canSetFeature(XMLUni::fgXercesSchemaFullChecking, true))
		parser->setFeature(XMLUni::fgXercesSchemaFullChecking, true);
	   
	   
	   
	
	XERCES_CPP_NAMESPACE::DOMDocument *doc = NULL;
	try 
	{
		Grammar *grammar = parser->loadGrammar(L"openmove.xsd", 1, true);
		doc = parser->parseURI(xmlFile);
	}
	catch (const XMLException& toCatch) 
	{
		char* message = XMLString::transcode(toCatch.getMessage());
		OGRE_EXCEPT(Exception::ERR_INTERNAL_ERROR, message, "CSceneBase::Create");
		XMLString::release(&message);
		return;
	}
	catch (const DOMException& toCatch) 
	{
		char* message = XMLString::transcode(toCatch.msg);
		OGRE_EXCEPT(Exception::ERR_INTERNAL_ERROR, message, "CSceneBase::Create");
		XMLString::release(&message);
		return;
	}
	catch (...) 
	{
		OGRE_EXCEPT(Exception::ERR_INTERNAL_ERROR, "Unexpected Exception", "CSceneBase::Create");
		return;
	}

	DOMNodeList *scenes = doc->getElementsByTagName(L"scene");
	XMLSize_t length = scenes->getLength();

	for (XMLSize_t i = 0; i < length; ++i)
	{
		DOMElement *scene = static_cast<DOMElement *>(scenes->item(i));
		XMLCh const *xmlType = scene->getAttribute(L"type");
		XMLCh const *xmlName = scene->getAttribute(L"name");
		XMLCh const *xmlPosition = scene->getAttribute(L"position");
		int position = XMLString::parseInt(xmlPosition);
		char * name = XMLString::transcode(xmlName);

		CSceneBase *sceneBase = NULL;
		if (0 == XMLString::compareString(L"CSceneBase", xmlType))
		{
			sceneBase = new CSceneBase(*engine, name, position);
		} else
		if (0 == XMLString::compareString(L"CSceneBlanche", xmlType))
		{
			sceneBase = new CSceneBlanche(*engine, name, position);
		} else
		if (0 == XMLString::compareString(L"CSceneCubenu", xmlType))
		{
			sceneBase = new CSceneCubenu(*engine, name, position);
		} else
		if (0 == XMLString::compareString(L"CSceneEcriture", xmlType))
		{
			sceneBase = new CSceneEcriture(*engine, name, position);
		} else
		if (0 == XMLString::compareString(L"CSceneFugana", xmlType))
		{
			sceneBase = new CSceneFugana(*engine, name, position);
		} else
		if (0 == XMLString::compareString(L"CSceneGlamPetitLoup", xmlType))
		{
			sceneBase = new CSceneGlamPetitLoup(*engine, name, position);
		} else
		if (0 == XMLString::compareString(L"CSceneInBed", xmlType))
		{
			sceneBase = new CSceneInBed(*engine, name, position);
		} else
		if (0 == XMLString::compareString(L"CSceneInBedHaut", xmlType))
		{
			sceneBase = new CSceneInBedHaut(*engine, name, position);
		} else
		if (0 == XMLString::compareString(L"CSceneLoupVoyeur", xmlType))
		{
			sceneBase = new CSceneLoupVoyeur(*engine, name, position);
		} else
		if (0 == XMLString::compareString(L"CScenePetitPot", xmlType))
		{
			sceneBase = new CScenePetitPot(*engine, name, position);
		} else
		if (0 == XMLString::compareString(L"CScenePluie", xmlType))
		{
			sceneBase = new CScenePluie(*engine, name, position);
		} else
		if (0 == XMLString::compareString(L"CScenePoursuite", xmlType))
		{
			sceneBase = new CScenePoursuite(*engine, name, position);
		} else
		if (0 == XMLString::compareString(L"CSceneRencontre", xmlType))
		{
			sceneBase = new CSceneRencontre(*engine, name, position);
		} else
		if (0 == XMLString::compareString(L"CSceneSouffle", xmlType))
		{
			sceneBase = new CSceneSouffle(*engine, name, position);
		} else
		if (0 == XMLString::compareString(L"CSceneTestFur", xmlType))
		{
			sceneBase = new CSceneTestFur(*engine, name, position);
		} else
		if (0 == XMLString::compareString(L"CSceneTestParticule", xmlType))
		{
			sceneBase = new CSceneTestParticule(*engine, name, position);
		} else
		if (0 == XMLString::compareString(L"CSceneTestRenaissance", xmlType))
		{
			sceneBase = new CSceneTestRenaissance(*engine, name, position);
		} else
		if (0 == XMLString::compareString(L"CSceneWater", xmlType))
		{
			sceneBase = new CSceneWater(*engine, name, position);
		}
		if (NULL != sceneBase)
		{
			sceneBase->Create(scene, static_cast<ENetConfig const>(netConfig));
			sceneMap.push_back(sceneBase);
		}
	}

	delete parser;
}
}

